var OPEN_ID = ''//储存获取到openid  
var SESSION_KEY = ''//储存获取到session_key 
const app = getApp()
//app.js
App({
 
  onLaunch: function (options) {
    if (!wx.getStorageSync('user')) {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
            wx.login({
              success: (res) => {
                if (res.code) {
                  wx.getUserInfo({
                    withCredentials: true,
                    success: (res_user) => {
                      wx.request({
                        url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/getOpenId',
                        data: {
                          code: res.code
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: (data) => {
                          if (data.data.success == 1) {
                            wx.request({
                              //后台接口地址
                              url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/login',
                              data: {
                                wx_openid: data.data.data.openid,
                                wx_headimgurl: res_user.userInfo.avatarUrl,
                                wx_nickname: res_user.userInfo.nickName
                              },
                              method: 'POST',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: (res) => {
                                if (res.data.success == 1) {
                                  this.globalData.userInfo = res.data.data;
                                  let user = {
                                    "openId": res.data.data.openId,
                                    "user_token": res.data.data.user_token
                                  }
                                  wx.setStorageSync("user", JSON.stringify(user))
                                  // wx.redirectTo({
                                  //   url: '/pages/index/index'
                                  // })
                                } else {
                                  wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                              }
                            })
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                            })
                          }

                        }
                      })
                    }, fail: function () {
                    }, complete: function (res) {
                    }
                  })
                }
              }
            })
          } else {
            // console.log(options.path);
            //拼接url的参数
            var urlWithArgs = ''

            for (var key in options) {
              var value = options[key]
              if(key=="query"){
                value = options[key].q
                urlWithArgs =  value;
              }
            
            }
            // urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

            console.log(urlWithArgs)
            wx.reLaunch({
              url: '/pages/logs/logs?re=' + urlWithArgs
            })
          }
        }
      })

    } else {
      wx.request({//检查过期
        url: 'https://lc.feiyouce.com/index.php?s=/api/CarUsers/userInfo',
        data: {
          'user_token': wx.getStorageSync("user").user_token
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          if (res.data.success == '1') {//token有效
            let timer = setTimeout(() => {
              clearTimeout(timer)
              // wx.redirectTo({
              //   url: '/pages/index/index'
              // })
            }, 1000)

          } else {//token 过期
            wx.login({
              success: (res) => {
                if (res.code) {
                  wx.getUserInfo({
                    withCredentials: true,
                    success: (res_user) => {
                      console.log(res_user)
                      wx.request({
                        url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/getOpenId',
                        data: {
                          code: res.code
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: (data) => {
                          if (data.data.success == 1) {
                            wx.request({
                              //后台接口地址
                              url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/login',
                              data: {
                                wx_openid: data.data.data.openid,
                                wx_headimgurl: res_user.userInfo.avatarUrl,
                                wx_nickname: res_user.userInfo.nickName
                              },
                              method: 'POST',
                              header: {
                                'content-type': 'application/x-www-form-urlencoded'
                              },
                              success: (res) => {
                                if (res.data.success == 1) {
                                  this.globalData.userInfo = res.data.data;
                                  let user = {
                                    "openId": res.data.data.openId,
                                    "user_token": res.data.data.user_token
                                  }
                                  wx.setStorageSync("user", JSON.stringify(user))
                                  // wx.redirectTo({
                                  //   url: '/pages/index/index'
                                  // })
                                } else {
                                  wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                  })
                                }
                              }
                            })
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                            })
                          }

                        }
                      })
                    }, fail: function () {
                    }, complete: function (res) {
                    }
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '菲速挪车',
      path: '/page/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
   setUserInfo:function(data){   //将用户信息缓存保存
    this.globalData.userInfo = data;
    this.globalData.user_token = data.user_token;
    wx.setStorage({
      key:"userInfo",
      data:data
    })
  },

  globalData: {
    user_token: null,
    userInfo:null,
    open_id: null,
    base_url: 'https://lc.feiyouce.com/index.php?s=/api'
  }
})